﻿<%@ WebHandler Language="C#" Class="HandlerSearchCV" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using nplJobsCommon.Common;
using nplJobsDAL.Common;
using System.Data;
using System.Linq;

public class HandlerSearchCV : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        if (context.Request["search"] != null)
        {
            if (context.Request["search"].ToString() == "city")
                GetCityList(context);
            else if (context.Request["search"].ToString() == "category")
                GetCategoryList(context);
            else if (context.Request["search"].ToString() == "skills")
                GetskillsList(context);
            else if (context.Request["search"].ToString() == "employmenttype")
                GetEmploymentTypeList(context);
            else if (context.Request["search"].ToString() == "qualification")
                GetQualificationList(context);
            else if (context.Request["search"].ToString() == "jobtitle")
                GetJobTitles(context);
            else if (context.Request["search"].ToString() == "categoryById")
                GetCategoryByID(context);
        }
    }

    private static void GetJobTitles(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        var list = new List<string>();
        DataTable dt = new nplJobsDAL.Common.JobTitleDAL().GetList(0);
        foreach (DataRow row in dt.Rows)
        {
            if (!list.Contains(row["Title"].ToString().ToUpper()))
                list.Add(row["Title"].ToString().ToUpper());
        }
        context.Response.Write(serializer.Serialize(list));
        
    }
    private static void GetCategoryList(HttpContext context)
    {       
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        nplJobsDAL.Select.SelectJobCategory categoryObj = new nplJobsDAL.Select.SelectJobCategory();

        List<JobCategory> listCity = categoryObj.GetJobCategory();
        List <string> listStr = new List<string>();        
        foreach (JobCategory j in listCity)
        {
            listStr.Add(j.Caterogy);
        }
        
        context.Response.Write(listStr.ToJSON());
    }

    private static void GetCategoryByID(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        nplJobsDAL.Select.SelectJobCategory categoryObj = new nplJobsDAL.Select.SelectJobCategory();

        List<JobCategory> listcat = categoryObj.GetJobCategory();
        string categoryName = string.Empty;
        
        foreach (JobCategory j in listcat)
        {
             if (context.Request["id"]!=null)
                if (context.Request["id"].ToString() == j.ID.ToString())
                    categoryName = j.Caterogy;
        }

        context.Response.Write(categoryName.ToJSON());
    }

    private static void GetCityList(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();       
        List<string> listCtype = new List<string>();
        nplJobsDAL.Common.JobSeekerPreesentAddress presentAddress = new nplJobsDAL.Common.JobSeekerPreesentAddress();
        DataTable dt = presentAddress.GetList();

        foreach (DataRow row in dt.Rows)
        {
            listCtype.Add(row["name"].ToString());
        }
        
        context.Response.Write(listCtype.ToJSON());
    }

    private static void GetskillsList(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();
       
        var list = new List<string>();
        nplJobsDAL.Common.SkillsDal dal = new nplJobsDAL.Common.SkillsDal();
        DataTable dt = dal.GetList();
        string[] skills = dt.Rows[0][0].ToString().Split(',');
        
        foreach (string s in skills)
        {
            if(!list.Contains(s.ToUpper()))
            list.Add(s.ToUpper());
        }
                
        context.Response.Write(list.ToJSON());
    }

    private static void GetEmploymentTypeList(HttpContext context)
    {
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            nplJobsDAL.Select.SelectJobType jTypeList = new nplJobsDAL.Select.SelectJobType();
            List<JobType> listType = jTypeList.GetJobType();
            List<string> listJtype = new List<string>();        
            foreach (JobType jt in listType)
            {           
                 listJtype.Add(jt.Type);
            }        
        
            context.Response.Write(listJtype.ToJSON());
    }

    private static void GetQualificationList(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        nplJobsDAL.Common.JobseekerDegreeQualification ql = new nplJobsDAL.Common.JobseekerDegreeQualification();
        DataSet ds = ql.GetListDegree();
        DataSet dsQ = ql.GetListQualification();
        string[]  qualification = dsQ.Tables[0].Rows[0][0].ToString().Split('~');
        DataTable dt = ds.Tables[0];
        
        List<string> listQtype = new List<string>();
        foreach (string s in qualification)
        {
            if (!listQtype.Contains(s.ToUpper()))
                listQtype.Add(s.ToUpper());  
        }
        foreach (DataRow r in dt.Rows)
        {
            if (!listQtype.Contains(r["Degree"].ToString().ToUpper()))
                listQtype.Add(r["Degree"].ToString().ToUpper());
        }
         
        context.Response.Write(listQtype.ToJSON());
    }
 
 
    public bool IsReusable {
        get {
            return false;
        }
    }

  
}